package cn.stard.user.controller;


import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import cn.stard.user.domain.SysDept;
import cn.stard.user.query.SysDeptQuery;
import cn.stard.user.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysDept")
public class SysDeptController {
    @Autowired
    public ISysDeptService sysDeptService;


    /**
     * 保存和修改公用的
     * @param sysDept  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SysDept sysDept){
        try {
            if( sysDept.getId()!=null)
                sysDeptService.update(sysDept);
            else
                sysDeptService.save(sysDept);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            sysDeptService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SysDept get(@PathVariable("id")Long id)
    {
        return sysDeptService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SysDept> list(){

        return sysDeptService.loadAll();
    }
    @PatchMapping
    public AjaxResult deles(@RequestBody List<Long> ids){
        try {

            sysDeptService.deles(ids);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SysDept> json(@RequestBody SysDeptQuery query)
    {
        return sysDeptService.queryData(query);
    }
}
